﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using JRCore.tools;
using System.IO;

namespace JRCore.protocol
{
    /// <summary>
    /// 杰瑞数据接收反馈协议V3.1
    /// </summary>
    public class DataRecvFeedBack
    {
        /// <summary>
        /// 参数个数13
        /// </summary>
        private const int N=13;
        /// <summary>
        /// 分隔符"_"
        /// </summary>
        private const string S = "_";
        /// <summary>
        /// 协议版本号
        /// </summary>
        public string P1;
        /// <summary>
        /// 传输标记(必须)0-上传失败 1-上传成功
        /// </summary>
        public string P2;
        /// <summary>
        /// 错误信息(可选)提示上传数据的错误信息
        /// </summary>
        public string P3;
        /// <summary>
        /// 预留1(可选)
        /// </summary>
        public string P4;
        /// <summary>
        /// 预留2(可选)
        /// </summary>
        public string P5;
        /// <summary>
        /// 预留3(可选)
        /// </summary>
        public string P6;
        /// <summary>
        /// 预留4(可选)
        /// </summary>
        public string P7;
        /// <summary>
        /// 预留5(可选)
        /// </summary>
        public string P8;
        /// <summary>
        /// 预留6(可选)
        /// </summary>
        public string P9;
        /// <summary>
        /// 预留7(可选)
        /// </summary>
        public string P10;
        /// <summary>
        /// 预留8(可选)
        /// </summary>
        public string P11;
        /// <summary>
        /// 预留9(可选)
        /// </summary>
        public string P12;
        /// <summary>
        /// 预留10(可选)
        /// </summary>
        public string P13;

        /// <summary>
        /// 反馈消息的空构造函数
        /// </summary>
        public DataRecvFeedBack()
        {
            P1 = "";
            P2 = "";
            P3 = "";
            P4 = "";
            P5 = "";
            P6 = "";
            P7 = "";
            P8 = "";
            P9 = "";
            P10 = "";
            P11 = "";
            P12 = "";
            P13 = "";
        }

        /// <summary>
        /// 由内存生成反馈消息对象
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>对象</returns>
        public static DataRecvFeedBack Parse(byte[] data)
        {
            DataRecvFeedBack ans = null;
            try
            {
                if (data != null)
                {
                    string txt = Encoding.UTF8.GetString(data);
                    if ((txt != null) && (txt != ""))
                    {
                        string[] ss = txt.Split('_');
                        if ((ss != null) && (ss.Length == N))
                        {
                            ans = new DataRecvFeedBack();
                            ans.P1 = ss[0];
                            ans.P2 = ss[1];
                            ans.P3 = ss[2];
                            ans.P4 = ss[3];
                            ans.P5 = ss[4];
                            ans.P6 = ss[5];
                            ans.P7 = ss[6];
                            ans.P8 = ss[7];
                            ans.P9 = ss[8];
                            ans.P10 = ss[9];
                            ans.P11 = ss[10];
                            ans.P12 = ss[11];
                            ans.P13 = ss[12];
                        }
                    }
                }
            }
            catch { ans = null; }
            return ans;
        }

        /// <summary>
        /// 反馈消息的字符串形式
        /// </summary>
        /// <returns></returns>
        public new string ToString()
        {
            return (P1 + S + P2 + S + P3 + S + P4 + S + P5 + S + P6 + S + P7 + S + P8 + S + P9 + S + P10 + S + P11 + S + P12 + S + P13);
        }

        /// <summary>
        /// 对象转换为bytes用于TCP发送
        /// </summary>
        /// <returns>内存数组</returns>
        public byte[] ToBytes()
        {
            byte[] ans = null;
            try
            {
                ans = Encoding.UTF8.GetBytes(this.ToString());
            }
            catch (Exception er)
            {
                ans = null;
                Console.WriteLine("DataRecvFeedBack.ToBytes()" + er.Message);
            }
            return ans;
        }
    }
}
